package com.jyh.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import com.jyh.config.BaseAction;
import com.jyh.db.entity.JyhButton;
import com.jyh.util.Utils;

@Service
public class ButtonService extends BaseAction {

	/**
	 * 按钮控件编写
	 * 
	 * @param cid
	 *            父级ID
	 * @param session
	 * @return
	 */
	public Object getButton(int cid, HttpSession session) {
		String button = getPackageButton(cid, session);
		return button;
	}

	/**
	 * 按钮控件编写
	 * 
	 * @param cid
	 *            父级ID
	 * @param condition
	 *            拼接按钮级条件
	 * @param json
	 *            追加按钮集合 当为页面级按钮非必填 List<T> 或list<Map> 转json
	 * @param session
	 * @return
	 */
	public Object getTableButton(int cid, String condition, String json, HttpSession session) {
		Object button = getPackageButton(cid, condition, json, session);
		return button;
	}

	/**
	 * 按钮拼装-input按钮
	 * 
	 * @param cid
	 *            父级ID
	 * @param session
	 * @return
	 */
	private String getPackageButton(int cid, HttpSession session) {
		// 获取数据库按钮
		Map<String, List<Object>> listSession = (Map<String, List<Object>>) session.getAttribute("buttonLimits");
		if (listSession == null) {
			return "";
		}
		// 获取权限
		Map<String, List<String>> listCenterSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		if (listCenterSession == null) {
			return "";
		}
		List<String> listCente = listCenterSession.get("left");
		// List<String> listCente = new ArrayList<String>();
		String projectName = (String) session.getAttribute("projectName");

		List<Object> list = listSession.get(cid + "_2_2");
		if (list == null || list.size() == 0) {
			return "";
		}
		String button = "";
		for (int i = 0; i < list.size(); i++) {
			JyhButton vo = (JyhButton) list.get(i);
			button += getButtonType1(vo.getId() + "", vo.getFunName(), vo.getLink(), projectName, listCente);
		}
		return button;
	}

	/**
	 * 按钮拼装-a按钮
	 * 
	 * @param list
	 * @return
	 */
	private Object getPackageButton(int cid, String condition, String json, HttpSession session) {
		// 获取数据库按钮
		Map<String, List<Object>> listSession = (Map<String, List<Object>>) session.getAttribute("buttonLimits");
		if (listSession == null) {
			return getJsonForListToMap(json);
		}
		// 获取权限
		Map<String, List<String>> listCenterSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		if (listCenterSession == null) {
			return getJsonForListToMap(json);
		}
		List<String> listCente = listCenterSession.get("left");
		// List<String> listCente = new ArrayList<String>();
		String projectName = (String) session.getAttribute("projectName");

		List<Map> list = getJsonForListToMap(json);

		List<Object> list_One = listSession.get(cid + "_1_1");
		List<Object> list_Two = listSession.get(cid + "_2_1");

		String button = "";
		if (list_Two != null && list_Two.size() != 0) {
			for (int i = 0; i < list_Two.size(); i++) {
				JyhButton vo = (JyhButton) list_Two.get(i);
				button += getButtonType2(vo.getId() + "", vo.getFunName(), vo.getLink(), condition, projectName,
						listCente);
			}
		}
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map) list.get(i);
			String button_List_One = "";
			if (list_One != null && list_One.size() != 0) {

				for (int j = 0; j < list_One.size(); j++) {
					JyhButton vo = (JyhButton) list_One.get(j);
					String isCondition = vo.getIsCondition();
					if (getIfButtonCondition(isCondition, map)) {
						button_List_One += getButtonType2(vo.getId() + "", vo.getFunName(), vo.getLink(), condition,
								projectName, listCente);
					}
				}
			}

			if (Utils.isNull(condition)) {
				if (!Utils.isNull(button_List_One + button)) {
					map.put("button", button_List_One + button + "");
				}

			} else {
				String botton_m = getButtonReplace(button_List_One + button, condition, map);
				if (!Utils.isNull(botton_m)) {
					map.put("button", botton_m + "");
				}
			}

		}

		return list;
	}

	/**
	 * button按钮
	 * 
	 * @param fun_name
	 * @param link
	 * @return
	 */
	private String getButtonType1(String id, String fun_name, String link, String projectName, List<String> listCente) {
		if (listCente.contains(id)) {
			return "<input type=\"button\" onclick=\"" + projectName + link + "\" value=\"" + fun_name + "\">";
		} else {
			return "";
		}
	}

	/**
	 * A标签控件
	 * 
	 * @param fun_name
	 *            空间中文名称
	 * @param link
	 *            连接地址
	 * @param condition
	 *            条件拼接
	 * @return
	 */
	private String getButtonType2(String id, String fun_name, String link, String condition, String projectName,
			List<String> listCente) {
		String button = "";
		if (!listCente.contains(id)) {
			return button;
		}
		if (!Utils.isNull(link) && !link.contains(".do")) {
			button = "<a href=\"javascript:void(0);\" onclick=\"" + getLinkCondition2(link, condition) + "\" >"
					+ fun_name + "</a>&nbsp;&nbsp;&nbsp;";
		} else {
			button = "<a href=\"" + projectName + getLinkCondition1(link, condition) + "\">" + fun_name
					+ "</a>&nbsp;&nbsp;&nbsp;";
		}
		return button;
	}

	/**
	 * a 标签条件及拼接
	 * 
	 * @param link
	 * @param condition
	 * @return
	 */
	private String getLinkCondition1(String link, String condition) {
		String linkRet = "";
		if (!"#".equals(link) && !Utils.isNull(condition)) {
			String linkCon = "";
			if (condition.contains(",")) {
				String[] strCondition = condition.split(",");
				for (int i = 0; i < strCondition.length; i++) {
					linkCon += strCondition[i] + "=[" + strCondition[i] + "]&&";
				}
				linkCon = linkCon.substring(0, linkCon.length() - 2);
			} else {
				linkCon += condition + "=[" + condition + "]";
			}
			linkRet = link + "?" + linkCon;

		}
		return linkRet;
	}

	/**
	 * a 标签按钮拼接
	 * 
	 * @param link
	 * @param condition
	 * @return
	 */
	private String getLinkCondition2(String link, String condition) {
		String linkRet = "";
		if (!"#".equals(link) && !Utils.isNull(condition)) {
			String linkCon = "";
			if (condition.contains(",")) {
				String[] strCondition = condition.split(",");
				for (int i = 0; i < strCondition.length; i++) {
					linkCon += "'[" + strCondition[i] + "]',";
				}
				linkCon = linkCon.substring(0, linkCon.length() - 1);
			} else {
				linkCon += "'[" + condition + "]'";
			}
			linkRet = link + "(" + linkCon + ")";
		}
		return linkRet;
	}

	/**
	 * 按钮参数替换
	 * 
	 * @param button
	 * @param condition
	 * @param map
	 * @return
	 */
	private String getButtonReplace(String button, String condition, Map map) {
		String buttonRet = new String(button);
		if (condition.contains(",")) {
			String[] strCondition = condition.split(",");
			for (int i = 0; i < strCondition.length; i++) {
				String str = map.get(strCondition[i]) + "";
				buttonRet = buttonRet.replace("[" + strCondition[i] + "]", str);
			}
		} else {
			buttonRet = buttonRet.replace("[" + condition + "]", map.get(condition) + "");
		}

		return buttonRet;
	}

	private Boolean getIfButtonCondition(String ifCondition, Map map) {
		if ("0".equals(ifCondition) || !ifCondition.contains("&")) {
			return true;
		}
		if (ifCondition.contains("!&")) {
			String[] ifCon = ifCondition.split("!&");
			if (!ifCon[0].equals(map.get(ifCon[1]) + "")) {
				return true;
			}
		} else if (ifCondition.contains("&")) {
			String[] ifCon = ifCondition.split("&");
			if (ifCon[0].equals(map.get(ifCon[1]) + "")) {
				return true;
			}
		} else {
			return false;
		}
		return false;
	}

}
